package com.zeldu.teacher.homework.service;

import com.alibaba.fastjson.JSON;
import com.zeldu.common.exception.CustomException;
import com.zeldu.common.model.response.CommonCode;
import com.zeldu.common.model.response.QueryResponseResult;
import com.zeldu.common.model.response.QueryResult;
import com.zeldu.common.model.response.ResponseResult;
import com.zeldu.teacher.homework.Dto.HomeWrokDetailDto;
import com.zeldu.teacher.homework.Dto.HomeworkDto;
import com.zeldu.teacher.homework.Dto.HomeworkDto_return;
import com.zeldu.teacher.homework.convert.HomeWrokDtoConvert;
import com.zeldu.teacher.homework.entity.HQList;
import com.zeldu.teacher.homework.entity.Student_HomeWrok;
import com.zeldu.teacher.homework.mapper.Student_HomeWrokMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class Student_HWorkService {
    @Autowired
    Student_HomeWrokMapper student_homeWrokMapper;


  public   QueryResponseResult<HomeworkDto_return> findAllMyHW(){
         List<Student_HomeWrok> homeWrokBySid = student_homeWrokMapper.findHomeWrokBySid(1286977770684948477L);
     //    List<HomeworkDto> homeworkDtos = HomeWrokDtoConvert.INSTANCE.SH2HomeworkDtoList(homeWrokBySid);

         List<HomeworkDto_return> res=new ArrayList<>();
      for (Student_HomeWrok s : homeWrokBySid) {
          HomeworkDto_return homeworkDto_return=new HomeworkDto_return();
          homeworkDto_return.setHomeworkId(s.getHomeworkId().toString());
          homeworkDto_return.setId(s.getId());
          homeworkDto_return.setScore(s.getScore());
          homeworkDto_return.setHomeworkName(s.getHomeworkName());
          homeworkDto_return.setStatus(s.getStatus());
          final String st = getTime(s.getStartTime());
          final String et = getTime(s.getEndTime());
          homeworkDto_return.setStartTime(st);
          homeworkDto_return.setEndTime(et);
          res.add(homeworkDto_return);
      }


        QueryResult<HomeworkDto_return> queryResult=new QueryResult<>();

        queryResult.setList(res);
        queryResult.setTotal(res.size());


         QueryResponseResult<HomeworkDto_return> responseResult=new QueryResponseResult<>(CommonCode.SUCCESS,queryResult);
        return responseResult;
    }
    private String getTime(Timestamp timestamp){
        final SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        final String format = simpleDateFormat1.format(timestamp);
        return format;
    }

    public QueryResponseResult<HomeWrokDetailDto> findDetailHW(Long hid) {

        final String detailHw = student_homeWrokMapper.findDetailHw(hid, 1286977770684948477l);
        final List<HomeWrokDetailDto> homeWrokDetailDtos = JSON.parseArray(detailHw, HomeWrokDetailDto.class);
        QueryResult<HomeWrokDetailDto> queryResult=new QueryResult<>();
        queryResult.setTotal(homeWrokDetailDtos.size());
        queryResult.setList(homeWrokDetailDtos);
        QueryResponseResult<HomeWrokDetailDto> responseResult=new QueryResponseResult<>(CommonCode.SUCCESS,queryResult);
        return responseResult;

    }
    @Transactional
    public ResponseResult recordHomeWrok(Long hid, String score) {
      if(hid==null||score==null||"".equals(score)){
          throw new CustomException(CommonCode.INVALID_PARAM);
      }
      Long sid=  1286977770684948477l;
        student_homeWrokMapper.UpdateScore(score,hid, sid);
        return new ResponseResult(CommonCode.SUCCESS);
    }
}
